package de.uni.muenster.mmm.AndroidCieLab;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import de.uni.muenster.mmm.AndroidCieLab.data.IMG;
import de.uni.muenster.mmm.AndroidCieLab.threads.InterpretPixelGT_I9100;
import de.uni.muenster.mmm.AndroidCieLab.threads.InterpretPixelHTCDesire;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class ManualCamSingle extends Activity{
	
	private Camera camera;
	private SurfaceView surfaceView;
	private FrameLayout frameLayout;
	
	private boolean finished = false;
	
	private Callback SurfaceViewCallback = new Callback() {

		public void surfaceDestroyed(SurfaceHolder holder) {}

		public void surfaceCreated(SurfaceHolder holder) {}

		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			try {
				camera.setPreviewDisplay(holder);
			} catch (IOException e) {
				Log.e("fehler", e.toString());
			}
		}
	};
	
	private Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
		
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,	data.length);
			
			ExecutorService executorService = Executors.newFixedThreadPool(2);
			
			IMG img = new IMG();
			
			Runnable thread = null;
			if(android.os.Build.MODEL.equals("HTC Desire")){
				thread = new InterpretPixelHTCDesire(img, bitmap, null);
			}
			else if(android.os.Build.MODEL.equals("GT-I9100")){
				thread = new InterpretPixelGT_I9100(img, bitmap, null);
			}
			
			if(thread != null){
				executorService.execute(thread);
				executorService.shutdown();
				while(!executorService.isTerminated() ){
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
			ImageView imageView = new ImageView(ManualCamSingle.this);
			imageView.setImageBitmap(bitmap);
			frameLayout.addView(imageView);
			
			TextView textView = new TextView(ManualCamSingle.this);
			textView.setTextColor(Color.RED);
			textView.setText("5: " + img.five + "\n10: " + img.ten + "\n20: " + img.twenty + "\n50: " + img.fifty + "\n100: " + img.hundred);
			Log.v("wichtig", "5: " + img.five + "\n10: " + img.ten + "\n20: " + img.twenty + "\n50: " + img.fifty + "\n100: " + img.hundred + android.os.Build.MODEL);
			frameLayout.addView(textView);
			
			frameLayout.removeView(surfaceView);
		}
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		camera = Camera.open();
		frameLayout = new FrameLayout(this);
		surfaceView = new SurfaceView(this);
		frameLayout.addView(surfaceView);
		SurfaceHolder holder = surfaceView.getHolder();
		holder.addCallback(SurfaceViewCallback);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		
		camera.startPreview();
		
		Parameters parameters = camera.getParameters();
		parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
		
		boolean hasMacro = false;
		List<String> possibleFocuses = parameters.getSupportedFocusModes();
		for (String string : possibleFocuses) {
			if(string.equals(Camera.Parameters.FOCUS_MODE_MACRO)){
				hasMacro = true;
			}
		}
		
		if(hasMacro){
			parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
		}
		else{
			parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
		}
		
		camera.setParameters(parameters);
		
		setContentView(frameLayout);
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN && finished == false){
			camera.takePicture(null, null, jpegCallback);
			finished = true;
		}
		return true;
	}
	
	
	@Override
	protected void onPause() {
		super.onPause();
		camera.stopPreview();
		camera.release();
		finish();
		
	}
}
